package com.pj.compose.utils

import android.annotation.SuppressLint
import android.bluetooth.*
import android.content.Context
import android.util.Log
import com.pj.compose.constants.BluetoothHidConstants
import java.util.concurrent.Executors

object BluetoothHidUtils {
    const val TAG = "Hid-Utils"
    var isConnected = false
    var isRegister = false
    var connectionStateChangeListener: ConnectionStateChangeListener? = null
    var bluetoothProfile: BluetoothProfile? = null
    var mDevice: BluetoothDevice? = null
    var mHidDevice: BluetoothHidDevice? = null

    @SuppressLint("MissingPermission")
    fun registerApp(context: Context?) {
        if (!isRegister) {
            val bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
            bluetoothAdapter.name = "MyBluetooth"
            bluetoothAdapter.getProfileProxy(
                context,
                mProfileServiceListener,
                BluetoothProfile.HID_DEVICE
            )
        }
    }

    private fun isConnected(_connected: Boolean) {
        isConnected = _connected
    }

    var mProfileServiceListener: BluetoothProfile.ServiceListener =
        object : BluetoothProfile.ServiceListener {
            @SuppressLint("MissingPermission", "NewApi")
            override fun onServiceDisconnected(profile: Int) {
                Log.e(TAG, "hid onServiceDisconnected")
                if (profile == BluetoothProfile.HID_DEVICE) {
                    mHidDevice!!.unregisterApp()
                }
            }

            @SuppressLint("NewApi", "MissingPermission")
            override fun onServiceConnected(profile: Int, proxy: BluetoothProfile) {
                Log.e(TAG, "hid onServiceConnected")
                bluetoothProfile = proxy
                if (profile == BluetoothProfile.HID_DEVICE) {
                    mHidDevice = proxy as BluetoothHidDevice
                    BluetoothHidConstants.HidDevice = mHidDevice
                    val sdp = BluetoothHidDeviceAppSdpSettings(
                        BluetoothHidConstants.NAME,
                        BluetoothHidConstants.DESCRIPTION,
                        BluetoothHidConstants.PROVIDER,
                        BluetoothHidDevice.SUBCLASS1_COMBO,
                        BluetoothHidConstants.Descriptor
                    )
                    mHidDevice!!.registerApp(
                        sdp,
                        null,
                        null,
                        Executors.newCachedThreadPool(),
                        mCallback
                    )
                }
            }
        }

    @SuppressLint("NewApi")
    val mCallback: BluetoothHidDevice.Callback = object : BluetoothHidDevice.Callback() {
        override fun onAppStatusChanged(pluggedDevice: BluetoothDevice, registered: Boolean) {
            Log.e(TAG, "onAppStatusChanged: $registered")
            isRegister = registered
        }

        override fun onConnectionStateChanged(device: BluetoothDevice, state: Int) {
            Log.e(TAG, "onConnectionStateChanged:$state")
            if (state == BluetoothProfile.STATE_DISCONNECTED) {
                isConnected(false)
                if (connectionStateChangeListener != null) {
                    connectionStateChangeListener!!.onDisConnected()
                    mDevice = null
                }
            } else if (state == BluetoothProfile.STATE_CONNECTED) {
                isConnected(true)
                mDevice = device
                if (connectionStateChangeListener != null) {
                    connectionStateChangeListener!!.onConnected()
                }
            } else if (state == BluetoothProfile.STATE_CONNECTING) {
                if (connectionStateChangeListener != null) {
                    connectionStateChangeListener!!.onConnecting()
                }
            }
        }
    }

    interface ConnectionStateChangeListener {
        fun onConnecting()
        fun onConnected()
        fun onDisConnected()
    }
}